/**
 * author:Administrator
 * date:2016/2/26
 * desc: 用户注册和登录
 * 1. 创建角色
 * 2. 根据登录结果，查找一个角色
 */

'use strict';
let authService = require('../infrastructure/service/authService.js');
let customerRepo = require('../domain/model_cmd/customer/customerRepo.js');
let vendorRepo = require('../domain/model_cmd/vendor/vendorRepo.js');
let managerRepo = require('../domain/model_cmd/manager/managerRepo.js');
let keeperRepo = require('../domain/model_cmd/shopkeeper/shopkeeperRepo.js');

let Customer = require('../domain/model_cmd/customer/Customer.js');
let Vendor = require('../domain/model_cmd/vendor/Vendor.js');
let Manager = require('../domain/model_cmd/manager/Manager.js');
let Shopkeeper = require('../domain/model_cmd/shopkeeper/Shopkeeper.js');

let service = {};
module.exports = service;

/**
 * 用户注册 name pwd
 */
service.registerUser = function*(name, pwd) {
  let user = yield authService.register(name, pwd);
  return user;
};

/**
 * 用户登录
 */
service.login = function*(name, pwd) {
  let user = yield authService.login(name, pwd);
  return user;
};

service.destroyUser = function*(uId, isForce) {
  yield authService.destroyUser(uId, isForce);
};

/**
 * 创建 customer
 * @param {Object} obj
 * @param {String} obj.name
 * @param {Number} obj.uId=undefined
 */
service.registerCustomer = function*(obj) {
  let customer = Customer.freshNew(obj);
  yield customer.store();
  return customer;
};

/**
 * 创建 Vendor
 * @param {Object} obj
 * @param {String} obj.name
 * @param {Number} obj.uId=undefined
 */
service.registerVendor = function*(obj) {
  let vendor = Vendor.freshNew(obj);
  yield vendor.store();
  return vendor;
};

/**
 * 创建 Manager
 * @param {Object} obj
 * @param {String} obj.name
 * @param {Number} obj.uId=undefined
 */
service.registerManager = function*(obj) {
  let manager = Manager.freshNew(obj);
  yield manager.store();
  return manager;
};

/**
 * 创建 shopkeeper
 * @param {Object} obj
 * @param {String} obj.name
 * @param {Number} obj.uId=undefined
 * @param {Number} obj.vendorId=undefined
 */
service.registerKeeper = function*(obj) {
  let keeper = Shopkeeper.freshNew(obj);
  yield keeper.store();
  return keeper;
};

/**
 * destroy role
 * @param {Number} uId
 * @param {Boolean} isForce
 */
service.destroyCustomer = function*(uId, isForce) {
  yield customerRepo.destroy(uId, isForce);
};

/**
 * destroy role
 * @param {Number} uId
 * @param {Boolean} isForce
 */
service.destroyVendor = function*(uId, isForce) {
  yield vendorRepo.destroy(uId, isForce);
};

/**
 * destroy role
 * @param {Number} uId
 * @param {Boolean} isForce
 */
service.destroyManager = function*(uId, isForce) {
  yield managerRepo.destroy(uId, isForce);
};

/**
 * destroy role
 * @param {Number} uId
 * @param {Boolean} isForce
 */
service.destroyShopkeeper = function*(uId, isForce) {
  yield keeperRepo.destroy(uId, isForce);
};
